package dart

class PersonController {
	def index = { redirect(action: listPeople) }

//--List People----------------------------------------------------------------
	def listPeople = {
		return params
	}

//--Edit Person----------------------------------------------------------------

	def editPerson = {
		Person person = Person.get(params.id)
		if (!person) {
			flash.message = "A person was not found with the id ${params.id}."
			redirect(action: listPeople)
			return
		}

		return [person: person]
	}

	def update = {
		Person person = Person.get(params.id)

		person.properties = params
		if (!person.hasErrors() && person.save(flush: true)) {
			redirect(action: listPeople)
		} else {
			flash.message = "There was a problem creating this person"
			flash.error = person.errors.fieldError
			render(view: 'editPerson', model: [person: person])
		}
	}

	def flipEnabled = {
		Person person = Person.get(params.id)

		person.enabled = !person.enabled
		if (person.save(flush: true)) {
			redirect(action: listPeople)
		} else {
			flash.message = "There was a problem changing this person"
			flash.error = person.errors.fieldError
			render(view: 'editClass', model: [person: person])
		}
	}

//--Create Person--------------------------------------------------------------
	def createPerson = {
		Person person = new Person(params)
		return [person: person]
	}

	def save = {
		Person person = new Person(params)
		if (!person.hasErrors() && person.save(flush: true)) {
			redirect(action: listPeople)
		} else {
			flash.message = "There was a problem creating this person"
			flash.error = person.errors.fieldError
			render(view: 'createPerson', model: [person: person])
		}
	}
}